<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php 
	$USER = getAccountUserObject();
	$ESCORT = $this->escort_entity->init('user_id',$USER->getId());
	$history_id = $this->uri->segment(4,0);
	 
	$PAYMENT_HIS = $this->payment_history->init('id',$history_id);
	
	if(!$PAYMENT_HIS OR $PAYMENT_HIS->user_id != $USER->getId() ){
		show_404();
	}
	 
		$invoice = $this->invoice_entity->init('history_id',$history_id);
		if(!$invoice){
			show_404();
		}
		
		$lines = $this->escort_invoice_lines_m->get_many_by('invoice_id', $invoice->id);
		$lineStr = '';
		$resultStr = '';
		$money = 0;
		if($lines){
			foreach ($lines as $line){
				$lineStr .= '<tr><td>'.$line->description.'</td><td>'.$line->quantity.'</td><td>'.$line->price.'€</td></tr>';
				$money += $line->price * $line->quantity;
			}
		}
		$resultStr = '<tr><td>'.$money.'€</td><td>'.round($money * 18/100, 2).'€</td><td>'.round($money*118/100).'€</td></tr>';
		if($invoice->user_id){
			$user = $this->user_entity->init('id', $invoice->user_id);
		}else{
			$user = null;
		}
		$this->load->library('pdf');
		// set document information
		// create new PDF document
		//$this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		$this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);
			
		
		// set document information
		$this->pdf->SetCreator(PDF_CREATOR);
		$this->pdf->SetAuthor('Highescort');
		$this->pdf->SetTitle('Highescort - Factura');
		$this->pdf->SetSubject('Highescort - Factura');
		$this->pdf->SetKeywords('Highescort - Factura, Factura');
		
		// set default header data
		//$this->pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 039', PDF_HEADER_STRING);
		
		// set header and footer fonts
		//$this->pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		//$this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		
		// set default monospaced font
		//$this->pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		
		//set margins
		$this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		
		//set auto page breaks
		$this->pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		
		//set image scale factor
		$this->pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
		
		//set some language-dependent strings
		//$this->pdf->setLanguageArray($l);
		
		// ---------------------------------------------------------
		
		//$this->pdf->SetFont('helvetica', 'B', 20);
		// add a page
		$this->pdf->AddPage();
		
		// set font
		
		//$this->pdf->Write(0, 'Example of HTML Justification', '', 0, 'L', true, 0, false, false, 0);
		
		// create some HTML content
		//$html = '<span style="text-align:justify;">a <u>abc</u> abcdefghijkl abcdef abcdefg <b>abcdefghi</b> a abc abcd  abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg <b>abcdefghi</b> a <u>abc</u> abcd abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg <b>abcdefghi</b> a abc abcd abcdef abcdefg abcdefghi a abc abcd <a href="http://tcpdf.org">abcdef abcdefg</a> start a abc before <span style="background-color:yellow">yellow color</span> after a abc abcd abcdef abcdefg abcdefghi a abc abcd end abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi a abc abcd abcdef abcdefg abcdefghi<br />abcd abcdef abcdefg abcdefghi<br />abcd abcde abcdef</span>';
		$html = $this->load->view('home/invoice_template', null, TRUE);
		
		$html = str_replace("###ID###", $invoice->id, $html);
		if($invoice->paid_date){
			$html = str_replace("###DATE###", format_date($invoice->paid_date), $html);
		}else{
			$html = str_replace("###DATE###", format_date($invoice->created_time), $html);
		}
		if($user){
			$html = str_replace("###CLIENT_NAME###", $user->name, $html);
			$html = str_replace("###CLIENT_ADDRESS###", $user->address, $html);
			$html = str_replace("###CLIENT_EMAIL###", $user->email, $html);
		}else{
			$html = str_replace("###CLIENT_NAME###", '', $html);
			$html = str_replace("###CLIENT_ADDRESS###", '', $html);
			$html = str_replace("###CLIENT_EMAIL###", '', $html);
		}
		$html = str_replace("###LINES###", $lineStr, $html);
		$html = str_replace("###RESULT###", $resultStr, $html);
		//$this->pdf->Ln();
		
		// output the HTML content
		$this->pdf->writeHTML($html, true, 0, true, true);
		
		// reset pointer to the last page
		$this->pdf->lastPage();
		
		// ---------------------------------------------------------
		
		//Close and output PDF document
		$this->pdf->Output('factura_'.$invoice->id.'.pdf', 'I');
		
		//============================================================+
		// END OF FILE                                                
		//============================================================+